﻿using Kz.Telegram.Bot.Console.HandleUpdate.MessageTypeHandler.MessageText;
using Kz.Telegram.Bot.Infrastructure;
using Kz.Telegram.Bot.Infrastructure.Extensions;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;

namespace Kz.Telegram.Bot.Console.HandleUpdate.MessageTypeHandler
{
    internal class MessageTextHandler : IHandler
    {
        readonly ILogger<MessageTextHandler> _logger = ServiceLocator.GetRequiredService<ILogger<MessageTextHandler>>();

        public async Task Handle(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
        {
            IHandler handler = default;
            var msg = update.Message!;

            if (msg.Entities == null)
            {
                switch (msg.Chat.Type!)
                {
                    case ChatType.Private:      //与机器人的私人对话的搜索引擎测试
                        handler = new SearchOfPrivateHandler();
                        break;
                    case ChatType.Group:
                        break;
                    case ChatType.Channel:
                        break;
                    case ChatType.Supergroup:   //与机器人的群聊对话的搜索引擎测试
                        handler = new SearchOfSupergroupHandler();
                        break;
                    case ChatType.Sender:
                        break;
                    default:
                        break;
                }
            }
            else
            {
                foreach (var entity in msg.Entities)
                {
                    switch (entity.Type)
                    {
                        case MessageEntityType.Mention:
                            break;
                        case MessageEntityType.Hashtag:
                            break;
                        case MessageEntityType.BotCommand:
                            {
                                handler = new MessageTextBotCommandHandler();
                            }
                            break;
                        case MessageEntityType.Url:
                            {
                                handler = new MessageTextUrlHandler();
                            }
                            break;
                        case MessageEntityType.Email:
                            break;
                        case MessageEntityType.Bold:
                            break;
                        case MessageEntityType.Italic:
                            break;
                        case MessageEntityType.Code:
                            break;
                        case MessageEntityType.Pre:
                            break;
                        case MessageEntityType.TextLink:
                            break;
                        case MessageEntityType.TextMention:
                            break;
                        case MessageEntityType.PhoneNumber:
                            break;
                        case MessageEntityType.Cashtag:
                            break;
                        case MessageEntityType.Underline:
                            break;
                        case MessageEntityType.Strikethrough:
                            break;
                        case MessageEntityType.Spoiler:
                            break;
                        case MessageEntityType.CustomEmoji:
                            break;
                        default:
                            break;
                    }
                }
            }

            try
            {
                if (handler == default)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    return;
                }
            }
            catch (OperationCanceledException ex)
            {
                _logger.LogWarning($"任务取消\r\n{ex.ToJson()}");
            }

            await handler!.Handle(botClient, update, cancellationToken);
        }
    }
}